<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
  <style>
    .selectInput {
      position: relative;
      width: 100%;
      min-width: 100px;
    }
    .selectInput .flavorSelect {
      position: absolute;
      width: 100%;
      padding: 0 10px;
      border-radius: 3px;
      border: solid 1px #FF903D;
      line-height: 30px;
      text-align: center;
      background: #fff;
      top: 50px;
      z-index: 99;
    }
    .selectInput .flavorSelect .items {
      cursor: pointer;
      display: inline-block;
      width: 100%;
      line-height: 35px;
      border-bottom: solid 1px #f4f4f4;
      color: #666;
    }
    .selectInput .flavorSelect .none {
      font-size: 14px;
    }

    #food-add-app .uploadImg .el-form-item__label::before{
      content: '*';
      color: #F56C6C;
      margin-right: 4px;
    }

  </style>
</head>
<body>
<div class="addBrand-container" id="food-add-app">
  <div class="container">
    <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            :inline="true"
            label-width="180px"
            class="demo-ruleForm"
    >
      <div>
        <el-form-item
                label="车辆品牌:"
                prop="brand"
        >
          <el-input
                  v-model="ruleForm.brand"
                  placeholder="请填写车辆品牌"
                  maxlength="20"
          />
        </el-form-item>
        <el-form-item
                label="颜色:"
                prop="colour"
        >
          <el-select
                  v-model="ruleForm.colour"
                  placeholder="颜色"
          >
            <el-option v-for="item in option2" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item
                label="车辆价格:"
                prop="price"
        >
          <el-input
                  v-model="ruleForm.price"
                  placeholder="请设置车辆价格"
          />
        </el-form-item>
      </div>
      <div>
        <el-form-item
                label="车辆性质:"
                prop="quality"
        >
          <el-select
                  v-model="ruleForm.quality"
                  placeholder="车辆性质"
          >
            <el-option v-for="item in option1" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item
                label="里程:"
                prop="mileage"
        >
          <el-input
                  v-model="ruleForm.mileage"
                  placeholder="请设置车辆里程"
          />
        </el-form-item>
      </div>
      <el-form-item
              label="车辆座位:"
              prop="seat"
      >
        <el-select
                v-model="ruleForm.seat"
                placeholder="车辆座位"
        >
          <el-option v-for="item in option3" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <div>
      </div>
      <div>
        <el-form-item
                label="车辆图片:"
                prop="region"
                class="uploadImg"
        >
          <el-upload
                  class="avatar-uploader"
                  action="/common/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :on-change="onChange"
                  ref="upload"
          >
            <img
                    v-if="imageUrl"
                    :src="imageUrl"
                    class="avatar"
            ></img>
            <i
                    v-else
                    class="el-icon-plus avatar-uploader-icon"
            ></i>
          </el-upload>
        </el-form-item>
      </div>
      <div class="subBox address">
        <el-form-item>
          <el-button @click="goBack()">
            取消
          </el-button>
          <el-button
                  type="primary"
                  @click="submitForm('ruleForm')"
          >
            保存
          </el-button>
          <el-button
                  v-if="actionType == 'add'"
                  type="primary"
                  class="continue"
                  @click="submitForm('ruleForm','goAnd')"
          >
            保存并继续添加车辆
          </el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/car.js"></script>
<script>
  new Vue({
    el: '#food-add-app',
    data() {
      return {
        id: '',
        restKey: 0,
        textarea: '',
        value: '',
        imageUrl: '',
        actionType: '',
        dishList: [],
        houseStyleData: [],
        houseTypeData: [],
        houseQualityData: [],
        vueRest : '1',
        index : 0,
        inputStyle  : {'flex':1},
        ruleForm  : {
          'brand': '',
          'colour':'',
          'seat':'',
          'quality':'',
          'price': '',
          'image': '',
          'mileage':''
        },
        mak: false,
        option1:[
          {
            value: '新',
            label: '新车',
          },
          {
            value: '旧',
            label: '二手车',
          },
        ],
        option2:[
          {
            value: '红',
            label: '红',
          },
          {
            value: '白',
            label: '白',
          },
          {
            value: '黄',
            label: '黄',
          },
          {
            value: '绿',
            label: '绿',
          },
          {
            value: '蓝',
            label: '蓝',
          },
          {
            value: '紫',
            label: '紫',
          },
          {
            value: '其',
            label: '其他',
          }
        ],
        option3:[
          {
            value: '2',
            label: '二座',
          },
          {
            value: '4',
            label: '四座',
          },
          {
            value: '6',
            label: '六座',
          },
          {
            value: '8',
            label: '八座',
          },
        ],
      }
    },
    computed: {
      rules () {
        return {
          'brand': [
            {'required': true, 'message': '请填写车辆品牌', 'trigger': 'blur'}
          ],
          'style': [
            {'required': true, 'message': '请选择车辆性质', 'trigger': 'change'}
          ],
          'price': [
            {
              'required': true,
              // 'message': '请填写菜品价格',
              validator: (rules, value, callback) => {
                if (!value) {
                  callback(new Error('请填写车俩价格'))
                } else {
                  const reg = /^\d+(\.\d{0,2})?$/
                  if (reg.test(value)) {
                    if(value > 0){
                      callback()
                    }else{
                      callback(new Error('价格小于0'))
                    }
                  } else {
                    callback(new Error('价格格式只能为数字,且最多保留两位小数'))
                  }
                }
              },
              'trigger': 'blur'
            }
          ],
        }
      }
    },
    created() {
      this.id = requestUrlParam('id')
      this.actionType = this.id ? 'edit' : 'add'
      if (this.id) {
        this.init()
      }
    },
    mounted() {
    },
    methods: {
      async init () {
        queryDishById(this.id).then(res => {
          console.log(res)
          if (String(res.code) === '1') {
            this.ruleForm = { ...res.data }
            this.ruleForm.price = String(res.data.price)
            // this.ruleForm.id = res.data.data.categoryId
            // this.imageUrl = res.data.data.image
            this.imageUrl = `/common/download?name=${res.data.image}`
          } else {
            this.$message.error(res.msg || '操作失败')
          }
        })
      },


      submitForm(formName, st) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = {...this.ruleForm}
            if(!this.imageUrl){
              this.$message.error('请上传车辆图片')
              return
            }
            if (this.actionType == 'add') {
              delete params.id
              addDish(params).then(res => {
                if (res.code === 1) {
                  this.$message.success('车辆添加成功！')
                  if (!st) {
                    this.goBack()
                  } else {
                    this.imageUrl = ''
                    this.ruleForm = {
                      'brand': '',
                      'colour':'',
                      'seat':'',
                      'quality':'',
                      'price': '',
                      'image': '',
                      'mileage':''
                    }
                  }
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            } else {
              delete params.updateTime
              editDish(params).then(res => {
                if (res.code === 1) {
                  this.$message.success('车辆修改成功！')
                  this.goBack()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            }
          } else {
            return false
          }
        })
      },

      handleAvatarSuccess (response, file, fileList) {
        // 拼接down接口预览
        if(response.code === 0 && response.msg === '未登录'){
          window.top.location.href = '/backend/page/login/login.html'
        }else{
          this.imageUrl = `/common/download?name=${response.data}`
          this.ruleForm.image = response.data
        }
      },

      onChange (file) {
        if(file){
          const suffix = file.name.split('.')[1]
          const size = file.size / 1024 / 1024 < 2
          if(['png','jpeg','jpg'].indexOf(suffix) < 0){
            this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
            this.$refs.upload.clearFiles()
            return false
          }
          if(!size){
            this.$message.error('上传文件大小不能超过 2MB!')
            return false
          }
          return file
        }
      },

      goBack(){
        window.parent.menuHandle({
          id: '4',
          url: '/backend/page/car/list.html',
          name: '车辆管理'
        },false)
      }
    }
  })
</script>
</body>
</html>